package com.zsinfo.guoss.supplier.utils;


import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import android.os.Environment;
import android.view.WindowManager;

import com.zsinfo.guoss.supplier.base.BaseActivity;

import java.io.File;
import java.io.FileOutputStream;
import java.text.DecimalFormat;


/**
 * by y on 2016/4/28.
 */
@SuppressWarnings("ALL")
public class UIUtils {

    public static Context getContext() {
        return BaseActivity.getContext();
    }

    public static Activity getActivity() {
        return BaseActivity.getActivity();
    }

    public static Drawable getDrawable(int id) {
        return getContext().getResources().getDrawable(id);
    }

    public static int getColor(int id) {
        return getContext().getResources().getColor(id);
    }

    public static String getString(int id) {
        return getContext().getResources().getString(id);
    }

    public static String[] getStringArray(int id) {
        return getContext().getResources().getStringArray(id);
    }

    /**
     * 文件夹是否存在
     * @param strFolder
     * @return
     */
    static boolean isFolderExists(String strFolder) {
        File file = new File(strFolder);
        if (!file.exists()) {
            if (file.mkdir()) {
                return true;
            } else
                return false;
        }
        return true;
    }

    /**
     * 裁剪图片大小 并且进行保存
     * @param path
     * @param size
     * @return
     */
    public static String saveBitmapP(String path,int size) {
        int sampleSize = 1;
        BitmapFactory.Options options = new BitmapFactory.Options();
        options.inJustDecodeBounds = true;
        BitmapFactory.decodeFile(path, options);
        int wid = options.outWidth;
        int hei = options.outHeight;
        if (wid > size || hei > size) {
            sampleSize = wid / size;
        }
        options.inSampleSize = sampleSize;
        options.inJustDecodeBounds = false;
        Bitmap bitmap=BitmapFactory.decodeFile(path, options);
        File file = null;
        try {

            if (isFolderExists(Environment.getExternalStorageDirectory() + "/gssps")) {
                file = new File(Environment.getExternalStorageDirectory()
                        + "/gssps/" + System.currentTimeMillis() + ".png");
                FileOutputStream outputStream = new FileOutputStream(file);
                bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);
                outputStream.flush();
                outputStream.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return file.getAbsolutePath();
    }

    public static void setBackgroundAlpha(Activity activity, float bgAlpha) {
        WindowManager.LayoutParams lp = activity.getWindow().getAttributes();
        lp.alpha = bgAlpha;
        if (bgAlpha == 1) {
            activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        } else {
            activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
        }
        activity.getWindow().setAttributes(lp);
    }

    /**
     * double型金额，保留两位小数
     */
    public static String formatDoule(String money) {
        double d = Double.parseDouble(money);
        if (d == 0.0) {
            return "0.00";
        }else if (d>0 && d<1){
            DecimalFormat df = new DecimalFormat("0.00");
            return df.format(d);
        } else {
            DecimalFormat df = new DecimalFormat("#.00");
            return df.format(d);
        }
    }

    /**
     * 得到软件显示版本信息
     *
     * @param context 上下文
     * @return 当前版本信息
     */
    public static String getVerName(Context context) {
        String verName = "";
        try {
            String packageName = context.getPackageName();
            verName = context.getPackageManager().getPackageInfo(packageName, 0).versionName;
        } catch (PackageManager.NameNotFoundException e) {
            e.printStackTrace();
        }
        return verName;
    }
}
